package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.pojo.Classs;
import com.zcf.utils.Hutool;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 商家商品分类表 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-12-12
 */
@Controller
@RequestMapping("/classs")
@CrossOrigin
@ResponseBody
public class ClasssController {
	/**
	 * 新增分类
	 */
	@PostMapping("inserClass")
	public Body inserClass(Classs class1) {
		class1.setAddtime(Hutool.parseDateToString());
		class1.setClassId(Hutool.getId());
		Boolean flag = class1.insert();
		if (flag) {
			return Body.newInstance(class1);
		}
		return Body.BODY_500;
	}

	/**
	 * 查询商家商品分类
	 */
	@PostMapping("queryClass")
	public Body queryClass(String dinId) {
		Classs classs = new Classs();
		List<Classs> lists = new ArrayList<>();
		EntityWrapper<Classs> ew = new EntityWrapper<>();
		ew.eq("din_id", dinId);
		lists = classs.selectList(ew);
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_500;
	}

	/**
	 * 删除分类
	 */
	@PostMapping("deleteClass")
	public Body deleteClass(String classId) {
		Classs classs = new Classs();
		classs.setClassId(classId);
		Boolean flag = classs.deleteById();
		if (flag) {
			return Body.newInstance("删除成功");
		}
		return Body.newInstance("删除失败");
	}

	/**
	 * 修改分类
	 */
	@PostMapping("updateClass")
	public Body updateClass(Classs classs) {
		classs.setModifytime(Hutool.parseDateToString());
		Boolean flag = classs.updateById();
		if (flag) {
			return Body.newInstance(classs);
		}
		return Body.newInstance("修改失败");
	}

}
